﻿
var remotebackPointer = function () {
    pointer.apply(this, arguments);

    this.listen(intent.action.callback, function (_this, _arg) {
        _this.o().receive(_arg);
    });
};

var remotebackHelper = function (jo) {
    var $this = this;
    this.listen = Utils.getGuid();
    jo.attr("listen", this.listen);
    this.group = Utils.getGuid();
    if (!jo.attr("remotegroup")) {
        jo.attr("remotegroup", this.group);
    } else {
        this.group = jo.attr("remotegroup");
    }
    var option = Utils.getAttrObject(jo, "remoteback");
    this.setting = $.extend({
        title: "选择",
        width: "90%",
        height: "90%"
    },
    option,
    {
        group: jo.attr("remotegroup") || 0
    });
    this.opendialog = function () {
        var dlgsettings = $.extend({
            type: "iframe",
            title: this.setting.title,
            url: this.setting.url,
            mask: true,
            resizeable: true,
            dragable: true,
            maxable: true,
            minable: false,
            width: this.setting.width,
            height: this.setting.height,
            intent: "select",
            frompointerid: this.listen
        }, option);
        mDialogManager.open(dlgsettings);
    };
    this.receive = function (data) {
        if (!data) return;
        $(".remotereset[remotegroup='" + $this.group + "']").show();
        var controls = $("[remotegroup='" + $this.group + "']");
        controls.each(function () {
            if ($(this).attr("remoteget")) {
                var getexpressioin = $(this).attr("remoteget");
                eval("var setvaluefn =function (o) { return " + getexpressioin + "}");
                var value = setvaluefn(data);
                if ($(this).is("input") || $(this).is("textarea")) {
                    $(this).val(value);
                } else {
                    $(this).html(value);
                }
            }
        });
    };
    this.reset = function () {
        $(".remotereset[remotegroup='" + this.group + "']").hide();
        var controls = $("[remotegroup='" + this.group + "']");
        controls.each(function () {
            if ($(this).is("input") || $(this).is("textarea")) {
                $(this).val("");
            } else {
                $(this).html("");
            }
        });
    }
    jo.click(function () {
        $this.opendialog();
    });
    $(".remotereset[remotegroup='" + this.group + "']").click(function () {
        $this.reset();
    });
    var p = new pointer($this.listen, $this);
    p.listen(intent.action.callback, function (_this, _arg) {
        _this.o().receive(_arg);
    });
    mWindowPointer.contain(p.id());
    mPointerManager.register(p);
};

(function ($) {
    $.fn.remoteback = function () {
        return this.each(function () {
            var helper=new remotebackHelper($(this));
        });
    }
})(jQuery);